package com.hotleproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotleproject.pojo.Hotle;
import com.hotleproject.service.AdminiOperaService;
import com.hotleproject.service.HotleService;
import com.hotleproject.service.RoomService;
import com.hotleproject.service.UsersService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/users")
public class HotleController {
    @Autowired
    private HotleService hotleService;
    //根据用户id查询用户5,修改controller
//日志操作
    @Autowired
    private AdminiOperaService adminiOperaService;
//    @RequestMapping("/findHotleAll")
//    public String findUserAll(Model model){
//        List<Hotle> list=this.hotleService.findAll();
//        model.addAttribute("list",list);
//        return  "pages_data";
//    }

//    @RequestMapping("/findHotleToReception")
//    public String findHotleToReception(Model model){
//        List<Hotle> list=this.hotleService.findAll();
//
//        model.addAttribute("list",list);
//        return  "HotleBook";
//    }

    @RequestMapping("/findHotleToReception")
    public String findHotleToReception(HttpServletRequest request, Model model) throws IOException {
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 10;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = hotleService.getCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;
        //计算总数量
        //分页查询
//        Map map=new HashMap();
//        map.put("pageNo",tempPageNo);
//        map.put("pageSize",pageSize);
//        map.put("phone",phone);
        List<Hotle> list = this.hotleService.pageList(tempPageNo, pageSize);

        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        return "HotleBook";
    }


    @RequestMapping("/searchHotle")
    public String searchHotle(HttpServletRequest request, Model model) throws IOException {
        String hotleCity = request.getParameter("hotleCity");
        model.addAttribute("city", hotleCity);
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 10;
        //页数
        int pageNo = 0;
        if (hotleCity == null) {
            hotleCity = "";
        }
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = hotleService.findHotlegetCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;
        //计算总数量
        //分页查询
//        Map map=new HashMap();
//        map.put("pageNo",tempPageNo);
//        map.put("pageSize",pageSize);
//        map.put("phone",phone);
        List<Hotle> list = this.hotleService.findHotleByHotleCity(hotleCity, tempPageNo, pageSize);

        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        return "HotleBook";
    }

    @RequestMapping("/findHotlePage")
    public String findHotlePage(HttpServletRequest request, Model model) throws IOException {
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 10;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count = hotleService.getCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;
        //计算总数量
        //分页查询
//        Map map=new HashMap();
//        map.put("pageNo",tempPageNo);
//        map.put("pageSize",pageSize);
//        map.put("phone",phone);
        List<Hotle> list = this.hotleService.pageList(tempPageNo, pageSize);

        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        return "pages_data";
    }


    private static final Logger LOGGER = LoggerFactory.getLogger(HotleController.class);


    //    @GetMapping("/upload")
//    public String upload() {
//        return "upload";
//    }
//上传文件
//    @PostMapping("/addHotle")
//    @ResponseBody
//    public String addHotle() {
//
//    }


    @RequestMapping("/addHotle")
    public String addHotle(@RequestParam("hotleImg") MultipartFile file,HttpSession session, HttpServletRequest request, Model model) throws IOException {

        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "E:/IntelliJ IDEA 2018.3.5/hotleproject/src/main/resources/static/manage/images/";
        File dest = new File(filePath + fileName);
        String name = filePath + fileName;
        try {
            file.transferTo(dest);
            this.hotleService.addHotle(request.getParameter("hotlename"),
                    request.getParameter("hotleCity"), request.getParameter("hotlePhone"),
                    fileName, request.getParameter("hotleDec"));

            ////日志操作

            String hotlename = request.getParameter("hotlename");
            String hotleCity = request.getParameter("hotleCity");
            String hotleDec = request.getParameter("hotleDec");
            String hotlePhone = request.getParameter("hotlePhone");

            String AdminiName = session.getAttribute("adminis").toString();
            String operationThing = "管理员" + AdminiName + "添加了酒店：" + request.getParameter("hotlename")
                    + "酒店地址：" + hotleCity + "。酒店电话：" + hotlePhone + "酒店描述：" + hotleDec + "。" ;
//        String operationDate=request.getParameter("operationDate");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String operationDate = df.format(new Date());
            this.adminiOperaService.insertope(AdminiName, operationThing, operationDate);

            return "redirect:/users/findHotlePage";
//            return "text";
        } catch (IOException e) {
//            LOGGER.error(e);

        }
        return "redirect:/users/findHotlePage";
    }


    @RequestMapping("/hotleUpdateGetHotlename")
    public String hotleUpdateGetHotlename(HttpServletRequest request, Model model) throws IOException {
//        this.hotleService.addHotle(hotle);
//        hotle.setHotleImg("/manage/images/" + hotle.getHotleImg());
// #{hotlename},#{hotleCity},#{hotlePhone},#{hotlePhone},'/manage/images/'+#{hotleImg},#{hotleDec})
        model.addAttribute("hotlename", request.getParameter("hotlename"));
        model.addAttribute("hotleImg", request.getParameter("hotleImg"));
        model.addAttribute("hotleCity", request.getParameter("hotleCity"));
        model.addAttribute("hotleDec", request.getParameter("hotleDec"));
        model.addAttribute("hotlePhone", request.getParameter("hotlePhone"));
        return "updateHotleHtml";
    }

    @RequestMapping("/updateHotle")
    public String updateHotle(HttpSession session, Hotle hotle, HttpServletRequest request, Model model) throws IOException {
//        this.hotleService.addHotle(hotle);
//        this.hotleService.updateHotle(hotle);
        String hotleImg;
        String rizhiImg;
        if (request.getParameter("hotleImg").toString().equals("")) {
            hotleImg = request.getParameter("hotleImgNull");
            rizhiImg = "图片未更新";
        } else {
            hotleImg = request.getParameter("hotleImg");
            rizhiImg = "图片已更新";
        }

//        hotle.setHotleImg("/manage/images/" + hotle.getHotleImg());
// #{hotlename},#{hotleCity},#{hotlePhone},#{hotlePhone},'/manage/images/'+#{hotleImg},#{hotleDec})
        String hotlename = request.getParameter("hotlename");
        String hotleCity = request.getParameter("hotleCity");
        String hotleDec = request.getParameter("hotleDec");
        String hotlePhone = request.getParameter("hotlePhone");
        String Beforehotlename = request.getParameter("Beforehotlename");
//        model.addAttribute("hotlename", request.getParameter("hotlename"));
//        model.addAttribute("hotleImg", request.getParameter("hotleImg"));
//        model.addAttribute("hotleImg", request.getParameter("hotleImgNull"));
//        model.addAttribute("hotleCity", request.getParameter("hotleCity"));
//        model.addAttribute("hotleDec", request.getParameter("hotleDec"));
//        model.addAttribute("hotlePhone", request.getParameter("hotlePhone"));

        this.hotleService.updateHotle(hotlename, hotleCity, hotlePhone, hotleImg, hotleDec, Beforehotlename);
////日志操作
        String AdminiName = session.getAttribute("adminis").toString();
        String operationThing = "管理员" + AdminiName + "更新了酒店：" + request.getParameter("hotlename")
                + "更新地址：" + hotleCity + "。更新电话：" + hotlePhone  + "。" + rizhiImg;
//        String operationDate=request.getParameter("operationDate");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String operationDate = df.format(new Date());
        this.adminiOperaService.insertope(AdminiName, operationThing, operationDate);
        return "redirect:/users/findHotlePage";
    }

    //
    @RequestMapping("/deleteHotle")
    public String deleteHotle(HttpSession session, HttpServletRequest request, Model model) throws IOException {
        String AdminiName = session.getAttribute("adminis").toString();

        String operationThing = "管理员" + AdminiName + "删除了酒店：" + request.getParameter("hotlename");
//        String operationDate=request.getParameter("operationDate");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String operationDate = df.format(new Date());
        this.adminiOperaService.insertope(AdminiName, operationThing, operationDate);
        model.addAttribute("hotlename", request.getParameter("hotlename"));
        this.hotleService.deleteHotle(request.getParameter("hotlename"));
        return "redirect:/users/findHotlePage";
//        return "text";
    }

}
